#pragma once
#include <highgui.h>
#include "Calibration.h"


namespace CreateCalibrationConfig
{
	void CreateFile(char** samplesFiles, int countSamples, char* outputFile, bool showImages = true, char* wnd = "wnd")
	{
		bool newWindow = (cvGetWindowHandle(wnd) == NULL) && showImages;
		if (newWindow)
			cvNamedWindow(wnd);

		Calibration::BoardCalibrator* c = NULL;

		for (int i = 0; i < countSamples; i++)
		{
			IplImage* img = cvLoadImage(samplesFiles[i]);
			if (c == NULL)
				c = new Calibration::BoardCalibrator(cvSize(img->width, img->height));

			c->AddImage(img);
			
			if (showImages)
			{
				cvShowImage(wnd, img);
				cvWaitKey(0);
			}

			cvReleaseImage(&img);
		}
		c->Calibrate();
		c->Save(outputFile);

		if (c)
			delete c;

		if (newWindow)
			cvDestroyWindow(wnd);
	}
};